package com.sky.spring.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.SQLException;

/**
 * Description:
 * 事务管理工具类 单例
 *  负责手动开启事务、提交事务、回滚事务
 * @author Sky
 * @Date 2021/7/31 11:58
 */
@Component("transactionManager")
public class TransactionManager {

    @Autowired
    private ConnectionUtils connectionUtils;

    // 手动开启事务
    public void beginTransaction() throws SQLException {
        connectionUtils.getCurrentThreadConn().setAutoCommit(false);
    }

    // 提交事务
    public void commit() throws SQLException {
        connectionUtils.getCurrentThreadConn().commit();
    }

    // 回滚事务
    public void rollBack() throws SQLException {
        connectionUtils.getCurrentThreadConn().rollback();
    }
}
